package com.eastmoney.emis.edda.projection;

import com.eastmoney.emis.edda.domain.event.EddiEvent;
import com.eastmoney.emis.edda.domain.event.EddiInstructionEvent;
import com.eastmoney.emis.edda.domain.repository.HsbcEddaFlowRepository;
import com.eastmoney.emis.edda.domain.repository.HsbcEddiFlowRepository;
import com.eastmoney.emis.edda.infrastructure.edda.po.EmhkEddaFlowPo;
import com.eastmoney.emis.edda.infrastructure.eddi.po.EmhkBankTrademidFlowPo;
import com.eastmoney.emis.logger.LoggerFactory;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

/**
 * @description: Description
 * @projectName: EDDA-HSBC
 * @package: com.eastmoney.emis.edda.domain
 * @author: 汪程程
 * @date: 2022/9/30 2:05 PM
 * @version: 1.0
 */
@Component
public class EddiEventListener {
    private static final Logger log = LoggerFactory.getLogger(EddiEventListener.class);

    @Autowired
    private HsbcEddiFlowRepository repository;

    @EventListener
    public void handlerEddiEvent(EddiEvent event) {
        log.info("client_id = {}, process event = {}", event.getClientId(), event);
        if (event instanceof EddiInstructionEvent) {
            EddiInstructionEvent instructionEvent = (EddiInstructionEvent) event;
            EmhkBankTrademidFlowPo flowPo = new EmhkBankTrademidFlowPo();
            flowPo.HKCUACCID = instructionEvent.getClientId();
            flowPo.OPTTYPE = instructionEvent.getOperation();
            flowPo.NOTE = instructionEvent.getNote();
            flowPo.APPLYREQ = instructionEvent.getApplyReq();
            flowPo.APPLYRES = instructionEvent.getApplyRes();
            flowPo.APPLYSERIALNO = instructionEvent.getApplySerialNo();
            flowPo.ERRORNO = instructionEvent.getErrorNo();
            flowPo.ERRORREASON = instructionEvent.getErrorReason();
            flowPo.STATUS = instructionEvent.getStatus();
            flowPo.MANDATEID = instructionEvent.getMandateId();
            flowPo.TRANSACTIONID = instructionEvent.getTransactionId();
            flowPo.REFEID = instructionEvent.getRefEid();
            flowPo.PROCESSUSERID = event.getProcessUid();
            flowPo.PROCESSUSERNAME = event.getProcessUserName();
            repository.addFlow(flowPo);
        }


    }

}
